package com.atguigu.schedule.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtil{
    private static ThreadLocal<Connection> threadLocal =new ThreadLocal<>();
    private static DataSource dataSource;
    //初始化连接池
    static {
//可以帮助我们读取.properties配置文件
        Properties properties = new Properties();
        InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /*1 向外提供连接池的方法*/
    public static DataSource getDataSource() {
        return dataSource;
    }
    /*2 向外提供连接的方法*/
    public static Connection getConnection(){
        Connection connection =threadLocal.get();
        if(null== connection){
            try {
                connection =dataSource.getConnection();
            }catch (SQLException e){
                throw  new RuntimeException(e);
            }
            threadLocal.set(connection);
        }
        return connection;
    }

    /*定义一个归还连接的方法(解除和ThreadLocal之间的关联关系)*/
    public static void releaseConnection(){
        Connection connection =threadLocal.get();
        if(null != connection){
            threadLocal.remove();
            //把连接设置回自动提交的连接
            try{
                connection.setAutoCommit(true);
                //自动归还到连接池
                connection.close();
            }catch(SQLException e){
                throw new RuntimeException(e);
            }
        }
    }
}